﻿
Imports sonicERPDAL
Imports sonicERPinfo
Imports System.Net
Imports System.Globalization.DateTimeFormatInfo
Imports System.Globalization
Imports System.Net.Dns
Imports System.Configuration
Imports System.Web.Configuration
Imports System.Web.Security

Public Class Login
    Inherits LoginBase

    Private Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
       
    End Sub

   

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            If Not IsPostBack Then
                FormsAuthentication.SignOut()
                HttpContext.Current.Session.Clear()
                HttpContext.Current.Session.Abandon()
                Dim cookie1 As HttpCookie = New HttpCookie(FormsAuthentication.FormsCookieName, "")
                Response.Cookies.Add(cookie1)
                Dim cookie2 As HttpCookie = New HttpCookie("ASP.NET_SessionId", "")
                cookie2.Expires = DateTime.Now.AddYears(-1)
                Response.Cookies.Add(cookie2)
                Response.Cache.SetCacheability(HttpCacheability.NoCache)
                HttpContext.Current.Request.Cookies.Clear()
                HttpContext.Current.Response.Cookies.Clear()
                HttpContext.Current.Application.Clear()
                HttpContext.Current.Session.RemoveAll()
            End If

        Catch ex As Exception
            lblerro.Text = ex.Message
        End Try


    End Sub


    Protected Sub Login2_Authenticate(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.AuthenticateEventArgs) Handles Login2.Authenticate
        Try


            If String.IsNullOrEmpty(Login2.UserName) Then
                Throw New Exception("Digite Seu nome")
            ElseIf String.IsNullOrEmpty(Login2.Password) Then
                Throw New Exception("Digite Sua Senha")
                'ElseIf Not String.IsNullOrEmpty(HttpContext.Current.Session("sessao")) Then
                '    Login1.FailureText = "Ja Existe uma Instacia da aplicação aberta"
                '    Throw New Exception("")
            End If
            'HttpContext.Current.Session("sessao") = "sessao"
            Dim usuario As String
            Dim senha As String
            Login2.Password.Trim()
            Login2.UserName.Trim()
            usuario = Login2.UserName.ToUpper
            senha = Login2.Password.ToUpper
            sessaousuario.usuario.apelido = usuario.ToUpper
            sessaousuario.usuario.senha = senha.ToUpper
            'Dim thisDate = DateTime.Now
            'Dim culture As New CultureInfo("pt-BR")
            'Dim databr As String = thisDate.ToString("d", culture)
            'entidade.dt_ultimo_acesso = Convert.ToDateTime(databr)

            sessaousuario.usuario.ip = ip()
            dal.usuario.pesquisa(sessaousuario.usuario)

            If usuario = sessaousuario.usuario.apelido1 And senha = sessaousuario.usuario.senha1 Then

                e.Authenticated = True

                FormsAuthentication.SetAuthCookie(Login2.UserName, False)
            Else
                e.Authenticated = False
            End If
        Catch ex As Exception
            pnl1.Visible = True
            lblerro.Text = ex.Message

        End Try
    End Sub

    Public Function ip()
        Dim ip_ As String = String.Empty
        Dim h As IPHostEntry = GetHostEntry(GetHostName)
        If h.AddressList.Length = 4 Then
            ip_ = CType(h.AddressList.GetValue(2), IPAddress).ToString()
        End If
        If String.IsNullOrEmpty(ip_) Then
            ip_ = String.Empty

        End If
        Return ip_




        'Dim uri_val As New Uri("http://meuip.datahouse.com.br/")
        'Dim reques As HttpWebRequest = HttpWebRequest.Create(uri_val)
        'reques.Method = WebRequestMethods.Http.Get
        'Dim response As HttpWebResponse = reques.GetResponse()
        'Dim reader As New IO.StreamReader(response.GetResponseStream())

        'Dim myIP As String = reader.ReadToEnd().ToString
        'Dim palavra As String() = myIP.Split(New Char() {" "c})

        'response.Close()
        'Dim lista As New ArrayList
        'For Each pa In palavra
        '    lista.Add(pa)

        'Next
        'Dim b As String = lista(15).ToString

        'b = b.Substring(0, 13)

        'Return b
    End Function



    

End Class